package com.dbh.company.buildbook;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.dbh.company.buildbook.bean.ResblockInfo;
import com.dbh.company.buildbook.bean.ResblockInfoReq;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author dongbanghui
 * @create 2023/11/8
 */
public class ProjectEntity {


    public static void main(String[] args) throws InterruptedException {


        downloadImage();


    }

    /**
     * select id, project_no,project_name
     * from dw.dw_ser_hdic_project_da a
     * where a.pt = "20231109000000" and a.project_no in (662309	)
     */
    public static void downloadImage () {
        ExcelReader reader = ExcelUtil.getReader("D:\\Administrator\\Downloads\\20277664.xlsx");
        List<Map<String, Object>> mapList = reader.readAll();
        reader.close();
        for (Map<String, Object> map : mapList) {
                Object id = map.get("id");
                String projectName = Convert.toStr(map.get("project_name"));
                String projectId = Convert.toStr(id);
                String projectNo = Convert.toStr(map.get("project_no"));
                File dir = FileUtil.mkdir("D:\\data\\work\\file\\楼书\\楼书1.0\\全景图\\" + projectName + "-" + projectNo);
                String url = "https://backend-mr.ke.com/v1/project-config/project-id/" + projectId;
                String body = HttpRequest.get(url)
                        .header("Cookie", "crosSdkDT2019DeviceId=-uudqok-p0z6tw-u5tozzmsej9s39l-t6zeqcw9i; ketest-env=production; _ga=GA1.2.2121015128.1686129466; wx_device_id=bUTx-Zs85VX-EJ38i6V8ZKBFvZtdlNd_7n7X1cq3OrE=; ke_uuid=f975736ecd03071f59f4d5c22f49dd01; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%22ce301845-8fe2-4a00-96fb-8f5ac09fbf35%22%2C%22first_id%22%3A%2218976818cbd94a-066522c3f35ba78-7e565470-2073600-18976818cbe2427%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E8%87%AA%E7%84%B6%E6%90%9C%E7%B4%A2%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC%22%2C%22%24latest_referrer%22%3A%22https%3A%2F%2Fcn.bing.com%2F%22%7D%2C%22%24device_id%22%3A%2218976818cbd94a-066522c3f35ba78-7e565470-2073600-18976818cbe2427%22%7D; casgwusercred=JeB0aqScXfnBqHHKMTc-uOGE18GJEkyUiXFoQzMHOVjFanqT2dWc39Cq9JY5r-XqiKMIi5dKAgkttLQ4sfNQO0-IJlnONP6B8iwab-wdIeNcuK0wUII6R-LENwLc-vVrdGgY007ggBs5k6P_f1Ox4YmvrUohxBl697JnH490ABc; crosgwusercred=P9_5unP3MN3GId8ENxx3RRnkjRrNJ_V-PnKvR2P1OHEyub_BsQTLzzhuHWyhi3KmPJ9blQxBy4nFKBK90hWSqw3d4dc60bdbd6081f86aefb678bd49e41; ketest-session=ioq8md9u4xivcwi9phg1ohjqa5cv9dwx; ketest-user=eyJsb2dpbl91c2VyIjogIjEwMDAwMDAwMjkxMzg0OTgiLCAiYWNjb3VudCI6ICJkb25nYmFuZ2h1aTAwMSIsICJhY2NvdW50U3lzdGVtSWQiOiAiZW1wbG95ZWUiLCAiYXZhdGFyIjogIm51bGwiLCAiYmlydGhkYXkiOiAibnVsbCIsICJidXNpbmVzc1Rva2VuIjogIjIuMDExMWE5YmViMjg0Mzg1YTlmMDAwMWNiNTQ1NzllNWZmZmI0ZDMwZTlmIiwgImNvcnBJZCI6ICJudWxsIiwgImRpc3BsYXlOYW1lIjogIlx1ODQ2M1x1NWUyZVx1OGY4OSIsICJlbWFpbCI6ICJkb25nYmFuZ2h1aTAwMUBrZS5jb20iLCAiZ2VuZGVyIjogIk1BTEUiLCAiaWQiOiAiMTAwMDAwMDAyOTEzODQ5OCIsICJtZmFBdXRoTWV0aG9kTGV2ZWwiOiAiMCIsICJwYXNzZWRNZmFSZWFzb24iOiAiaXBXaGl0ZUxpc3QiLCAicGhvbmUiOiAiMTg1MDE5OTI1OTQiLCAicmVhbE5hbWUiOiAiXHU4NDYzXHU1ZTJlXHU4Zjg5IiwgInNlcnZpY2VJZCI6ICJmYW1pbHkua2UuY29tIiwgInN0YXRlIjogIk5PUk1BTCIsICJ0Z3RJZCI6ICJUR1QtMTgxMDcxNi1NV20yWktvMkg2aHBKR3U0VHhac0pTbUhhcXBkb1Zxa1RHbmVvY2oyUGMzZ0lKcmlxbEVZU294Q0djRU9HbU5TY3Y4LWtlLmNvbSIsICJ1Y2lkIjogIjEwMDAwMDAwMjkxMzg0OTgiLCAidWNuYW1lIjogIlx1ODQ2M1x1NWUyZVx1OGY4OSIsICJ1aWQiOiAiMTAwMDAwMDAyOTEzODQ5OCIsICJ1c2VyX2NvZGUiOiAiMjkxMzg0OTgiLCAidXNlclRhZyI6ICIwIiwgInZlcnNpb24iOiAiODY5IiwgInpoaWRhb191Y2lkIjogIjIxMTAwMDAwMDQwOTY4MDkiLCAibmV4dF91cmwiOiAiaHR0cDovL2tldGVzdC1wcm8ua2UuY29tLyMvdG9vbC9kYXl1Q2hhdGJvdCJa2V0ZXN0LXVzZXI=9; _ga_F0DQ6TTSVW=GS1.2.1699427842.52.1.1699428872.60.0.0; beikeBaseData=%7B%22parentSceneId%22%3A%22%22%7D; mr_backend_service_uid=1000000029138498; login_ucid=1000000029138498; security_ticket=jc4EKXqYxI+VyMNUqWraPjTDEtfstrqfoUDEBv1l+so/oHRv5LIHi0TDYZkN59T6Sd8eEMSCxT5uFSWrQGRsA3DA5DK+c40RDWwoauY3HqJy0dmAwB/aQBMry74XodJ0EMYMXfOhY6hrr5qAblETT2gt4RFTkW8ReK/H2kP7Qz8=; factory_token=2.0111a9beb284385a9f0001cb54b1b28fa70803ee56; lianjia_token=2.0013e349c78672adea024af775a390fad8; mr_backend_login_user=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzZXNzaW9uSWQiOiIyLjAwMTU3MzA3OGQ4MGUyZTNhMDA0ZGFiOTNmNTNmNDEyYWEifQ.jzZEIxvrvmX3Gy2oEZFaaKxUlCl6Rf6_pqWIv7_PZLM; lianjia_uuid=b5f09b95-90af-4594-9e66-310cc6207a3a; lianjia_ssid=4e8788a5-8c34-4d99-876a-a3803b414c95")
                        .execute()
                        .body();
                JSONObject jsonObject = JSONUtil.parseObj(body);
                JSONArray data = jsonObject.getJSONArray("data");
                for (Object datum : data) {
                    String jsonStr = JSONUtil.toJsonStr(datum);
                    JSONObject jsonObject1 = JSONUtil.parseObj(jsonStr);
                    Object o = jsonObject1.get("panoramaFileUrl");
                    String str = Convert.toStr(o);
                    // 下载
                    HttpUtil.downloadFile(str,  dir );
                }
                Console.log("项目{}下载完成", projectId);
        }

    }

    private static void exportExcelData() throws InterruptedException {
        ExcelWriter writer = ExcelUtil.getBigWriter("D:\\data\\work\\file\\楼书\\楼书1.0\\1.xlsx");

        writer.renameSheet("数据");
        // 获取标题行样式
        CellStyle headCellStyle = writer.getHeadCellStyle();
        //水平居中
        headCellStyle.setAlignment(HorizontalAlignment.CENTER);
        //垂直居中
        headCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 文本格式cellStyle
        DataFormat textFormat = writer.getWorkbook().createDataFormat();
        CellStyle textCellStyle = writer.getWorkbook().createCellStyle();
        textCellStyle.setDataFormat(textFormat.getFormat("@"));
        List<ResblockInfo> list =  new ArrayList<>();
        for (int  i = 0; i  < 42; i++) {
            ResblockInfoReq resblockInfoReq = new ResblockInfoReq();
            resblockInfoReq.setCityId(12);
            resblockInfoReq.setPageNum(i + 1);
            resblockInfoReq.setPageSize(100);
            List<ResblockInfo> projectList = null;
            try {
                projectList = getProjectList(resblockInfoReq);
            } catch (Exception e) {
                Console.error(i + 1 + "页出错,{}", JSONUtil.toJsonStr(resblockInfoReq));
            }
            list.addAll(projectList);
            Thread.sleep(1000 * 2);
        }


        writer.write(list, true);
        writer.close();
    }

    public static List<ResblockInfo> getProjectList(ResblockInfoReq re) {
        String url = "https://pre-mr.ke.com/api/v1/getResblockInfo";
        String body = HttpRequest.post(url)
                .body(JSONUtil.toJsonStr(re))
                .execute()
                .body();
        JSONObject jsonObject = JSONUtil.parseObj(body);
        Object o = jsonObject.get("data");
        String jsonStr = JSONUtil.toJsonStr(o);
        JSONArray objects = JSONUtil.parseArray(jsonStr);
        List<ResblockInfo> resblockInfos = new ArrayList<>();
        objects.forEach(item -> {
            ResblockInfo bean = BeanUtil.toBean(item, ResblockInfo.class);
            resblockInfos.add(bean);
        });

        return resblockInfos;
    }



}
